import json
import os

class JSONDBSD:
    def __init__(self, filename):
        self.lockFile = filename + '.lock'
        self.load_data(filename)

    def load_data(self, filename):
        if os.path.exists(filename):
            try:
                with open(filename, 'r') as file:
                    json_data = file.read()
                    if json_data:
                        self.data = json.loads(json_data)
                        self.cache = self.data
                    else:
                        self.data = {}
                        self.cache = {}
            except json.JSONDecodeError:
                self.data = {}
                self.cache = {}
        else:
            self.data = {}
            self.cache = {}

    def save_data(self, filename):
        json_data = json.dumps(self.data)
        with open(filename, 'w') as file:
            file.write(json_data)

    def insert(self, table, data):
        if table not in self.data:
            self.data[table] = []
            self.cache[table] = []
        self.data[table].append(data)
        self.cache[table].append(data)
        self.save_data(self.lockFile)

    def delete(self, table, condition):
        if table in self.data:
            self.data[table] = [row for row in self.data[table] if self.check_conditions(row, condition)]
            self.cache[table] = self.data[table]
            self.save_data(self.lockFile)

    def select(self, table, condition):
        if table in self.cache:
            result = []
            for row in self.cache[table]:
                if self.check_conditions(row, condition):
                    result.append(row)
            return result
        else:
            return []

    def update(self, table, condition, data):
        if table in self.data:
            for row in self.data[table]:
                if self.check_conditions(row, condition):
                    for key, value in data.items():
                        row[key] = value
            self.cache[table] = self.data[table]
            self.save_data(self.lockFile)

    def check_conditions(self, row, conditions):
        result = True
        for field, comparison in conditions.items():
            comparison_operator = list(comparison.keys())[0]
            comparison_value = comparison[comparison_operator]
            if comparison_operator == '=':
                comparison_result = row[field] == comparison_value
            elif comparison_operator == '!=':
                comparison_result = row[field] != comparison_value
            elif comparison_operator == '<':
                comparison_result = row[field] < comparison_value
            elif comparison_operator == '>':
                comparison_result = row[field] > comparison_value
            elif comparison_operator == '<=':
                comparison_result = row[field] <= comparison_value
            elif comparison_operator == '>=':
                comparison_result = row[field] >= comparison_value
            elif comparison_operator == 'contains':
                comparison_result = comparison_value in row[field]
            else:
                comparison_result = False
            result = result and comparison_result
        return result

# 创建JSONDBSD对象
db = JSONDBSD('data.json')

# 打开data.json文件并加载数据
db.load_data('data.json')
print(db.data)

# 修改数据
db.insert('users', {'id': 1, 'name': 'John Doe', 'age': 25})
db.insert('users', {'id': 2, 'name': 'Jane Smith', 'age': 30})

# 查询数据
result = db.select('users', {'age': {'>': 25}})
print(result)
# 输出: [{'id': 2, 'name': 'Jane Smith', 'age': 30}]

# 更新数据
db.update('users', {'id': {'=': 1}}, {'age': 26})

# 删除数据
db.delete('users', {'age': {'<': 30}})

# 查询更新后的数据
result = db.select('users', {})
print(result)
# 输出: [{'id': 1, 'name': 'John Doe', 'age': 26}]

# 保存数据到data.json文件
db.save_data('data.json')